﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace House_World
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Camera camera;
        VertexBuffer[] buffer;
        VertexBuffer textureTerrain;
        VertexPositionColor[] house;
        Texture2D texture;
        Texture2D destiny;
        Models3D ModelShip;
        BasicEffect textureColor;
        Sort ordenation;
        Billboard[] billboard = new Billboard[20];
        Mastro mastro;
        Terrain terrain;
        Texture2D texture2;
        BasicEffect worldNave;
        Matrix rot = Matrix.Identity;
        Helice helice;
        Dragon dragon;
        SpriteFont sFont;
        string actualSort = "Mode : ";
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            InstanceBillboard();
            mastro = new Mastro(GraphicsDevice); 
            base.Initialize();
        }

        protected override void LoadContent()
        {
            camera = new Camera(this, new Vector3(0, 3, 30));
            spriteBatch = new SpriteBatch(GraphicsDevice);
            texture = Content.Load<Texture2D>(@"Texture\mato");
            texture2 = Content.Load<Texture2D>(@"Texture\tree");
            destiny= Content.Load<Texture2D>(@"Texture\neve");
            ModelShip = new Models3D(Content.Load<Model>(@"spaceship"),GraphicsDevice);
            helice = new Helice(GraphicsDevice);
            terrain = new Terrain(GraphicsDevice, camera, texture,Content);
            House cb = new House(Color.Blue);
            dragon = new Dragon(Window, Content, GraphicsDevice);
            house = cb.returnCube();

            buffer = new[]
            {
                   new VertexBuffer(GraphicsDevice,typeof(VertexPositionColor),house.Length,BufferUsage.None),  
                   new VertexBuffer(GraphicsDevice,typeof(VertexPositionColor),helice.verts.Length,BufferUsage.None),
                   new VertexBuffer(GraphicsDevice,typeof(VertexPositionColor),mastro.verts.Length,BufferUsage.None),
            };

            textureTerrain = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), terrain.verts.Length, BufferUsage.None);
            textureTerrain.SetData<VertexPositionTexture>(terrain.verts);
            buffer[0].SetData<VertexPositionColor>(house);
            buffer[1].SetData<VertexPositionColor>(helice.verts);
            buffer[2].SetData<VertexPositionColor>(mastro.verts);
            textureColor = new BasicEffect(GraphicsDevice);
            spriteBatch = new SpriteBatch(GraphicsDevice);
            worldNave = new BasicEffect(GraphicsDevice);
            worldNave.World *= Matrix.CreateTranslation(new Vector3(15, 0, -50));
            sFont = Content.Load<SpriteFont>(@"SpriteFont1");
        }

        protected override void UnloadContent()
        {

        }
        protected override void Update(GameTime gameTime)
        {
            KeyboardState ks = Keyboard.GetState();
            if (ks.IsKeyDown(Keys.E))
            {
            ordenation = new QuickSort();
            actualSort = "Mode : QuickSort";
            }
            if (ks.IsKeyDown(Keys.R))
            {
            ordenation = new BubbleSort();
            actualSort = "Mode : BubbleSort";
            }
            if (ks.IsKeyDown(Keys.T))
            {
            ordenation = new MergeSort();
            actualSort = "Mode : MergeSort";
            }
            if (ks.IsKeyDown(Keys.Y))
            {
            ordenation = new HeapSort();
            actualSort = "Mode : HeapSort";
            }

            camera.Update();
            helice.Rotate();
            dragon.Update(gameTime);
            terrain.Update(gameTime);
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);
        }
        public void InstanceBillboard()
        {
            billboard[0] = new Billboard(GraphicsDevice, Window, Content, 0, 0.5f);
            billboard[1] = new Billboard(GraphicsDevice, Window, Content, 1, 0.5f);
            billboard[2] = new Billboard(GraphicsDevice, Window, Content, 2, 0.5f);
            billboard[3] = new Billboard(GraphicsDevice, Window, Content, 3, 0.5f);
            billboard[4] = new Billboard(GraphicsDevice, Window, Content, 4, 0.5f);
            billboard[5] = new Billboard(GraphicsDevice, Window, Content, 0, 1f);
            billboard[6] = new Billboard(GraphicsDevice, Window, Content, 1, 1f);
            billboard[7] = new Billboard(GraphicsDevice, Window, Content, 2, 1f);
            billboard[8] = new Billboard(GraphicsDevice, Window, Content, 3, 1f);
            billboard[9] = new Billboard(GraphicsDevice, Window, Content, 4, 1f);
            billboard[10] = new Billboard(GraphicsDevice, Window, Content, 0, 1.5f);
            billboard[11] = new Billboard(GraphicsDevice, Window, Content, 1, 1.5f);
            billboard[12] = new Billboard(GraphicsDevice, Window, Content, 2, 1.5f);
            billboard[13] = new Billboard(GraphicsDevice, Window, Content, 3, 1.5f);
            billboard[14] = new Billboard(GraphicsDevice, Window, Content, 4, 1.5f);
            billboard[15] = new Billboard(GraphicsDevice, Window, Content, 0, 2f);
            billboard[16] = new Billboard(GraphicsDevice, Window, Content, 1, 2f);
            billboard[17] = new Billboard(GraphicsDevice, Window, Content, 2, 2f);
            billboard[18] = new Billboard(GraphicsDevice, Window, Content, 3, 2f);
            billboard[19] = new Billboard(GraphicsDevice, Window, Content, 4, 2f);
           
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.RasterizerState = RasterizerState.CullNone;
            GraphicsDevice.Clear(Color.CornflowerBlue);
            textureColor.World = Matrix.Identity;
            textureColor.View = camera.View;
            textureColor.Projection = camera.Projector;
            textureColor.VertexColorEnabled = true;
            GraphicsDevice.SetVertexBuffer(buffer[1]);
            
            terrain.Draw(GraphicsDevice, textureTerrain, destiny, camera);
          
            ModelShip.Draw(camera);
            if (ordenation!=null)
            {
            ordenation.Sort(billboard);
            }
            dragon.Draw(GraphicsDevice, camera);

            foreach (EffectPass pass in textureColor.CurrentTechnique.Passes)
            {
                pass.Apply();
                GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, house, 0, 10);
            }
            for (int x = 0; x <20; x++)
            {     
                    billboard[x].Draw(GraphicsDevice, camera);     
            }

            
            mastro.Draw(camera, GraphicsDevice);
            helice.Draw(GraphicsDevice, camera);

            spriteBatch.Begin();
            spriteBatch.DrawString(sFont, "E - QuickSort / R - BubbleSort / T MergeSort / Y - HeapSort", new Vector2(0f, 50.0f), Color.White);
            spriteBatch.DrawString(sFont, actualSort, new Vector2(0f, 200.0f), Color.White); spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
